//
//  CommentCollectionCell.m
//  MedicineBox
//
//  Created by dllo on 15/9/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CommentCollectionCell.h"
#import "PurchaseCell.h"
#import "MJRefreshAutoNormalFooter.h"
#import "MJRefreshNormalHeader.h"
#import "AsyncConnection.h"
#import "InformationCell.h"
#import "DKNightVersion.h"
#import "DKNightVersion.h"
@interface CommentCollectionCell ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, assign) int count;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) UITableView *tableV;

@end
@implementation CommentCollectionCell

- (void)dealloc
{
    
    [_dic release];
    [_tableV release];
    [_tableArray release];
    [super dealloc];
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
        self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.nightBackgroundColor = UIColorFromRGB(0x343434);
        [self.contentView addSubview:_tableV];
        
    }
    return self;
}

- (void)getArray:(NSDictionary *)dic str:(NSString *)latterStr idstr:(NSString *)idStr
{

    
    _count = 1;
    
    [_tableArray removeAllObjects];
    self.dic = dic;
    [self.tableArray addObjectsFromArray:[dic objectForKey:@"details"]];
    [self.tableV reloadData];
    
    [self shuanXinJiaZai:latterStr idStr:idStr];
    

}

- (void)getId:(NSString *)siteId
{

    if ([_type isEqualToString:@"information"]) {
        
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.backgroundColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
        _tableV.nightBackgroundColor = UIColorFromRGB(0x343434);
        
    }
    NSString *site = [NSString stringWithFormat:@"http://59945590.146.shengyiquan.org/requestdata.aspx?action=GetNewsDetail&NewsID=%@",siteId];
    [AsyncConnection asyncConnectionWithUrl:site name:nil parameters:nil block:^(id data) {
    
        self.dic = data;
        [self.tableV reloadData];
        
    } get:@"get"];

}

- (void)shuanXinJiaZai:(NSString *)latterStr idStr:(NSString *)idStr
{

    
    
    self.tableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _count++;
        NSString *freshSite = [NSString stringWithFormat:@"http://mobi.111.com.cn/ApiControl?method=product.review&pId=%@&category=%@&pageId=%d&pageSize=2147483647&encryptversion=2&sign=30043B750BEB08BDA78226E773BBB770&timestamp=20150926123919&os=android&venderId=2011102716210000&signmethod=md5&format=json&type=mobile&channelName=huawei&versionName=4.6.1&versionCode=477&screensize=480*854&width=480&height=854",idStr,latterStr,_count];
       
        [AsyncConnection asyncConnectionWithUrl:freshSite name:nil parameters:nil block:^(id data) {
            
            NSArray *array = [[[data objectForKey:@"data"] objectForKey:@"review"] objectForKey:@"details"];
            [_tableArray addObjectsFromArray:array];
            
        } get:@"post"];
      
        [self.tableV.footer endRefreshing];
        
    }];
    

   

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([_type isEqualToString:@"comment"]) {
        
        static NSString *cellI = @"cellId";
        PurchaseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];
        if (cell == nil) {
            
            cell = [[PurchaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellI];
            
        }
        if (_tableArray.count != 0) {
            
            
            [cell getItem:[_tableArray objectAtIndex:indexPath.row] bigDic:nil];
            
        }
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        return cell;
        
    } else if ([_type isEqualToString:@"information"]) {
    
        static NSString *cellI = @"info";
        InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];
        if (cell == nil) {
            
            cell = [[InformationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellI];
            
        }
       
        if (_dic != nil) {
            
            [cell getDic:_dic];
            
        }
        cell.backgroundColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
        cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        
        return cell;
    
    }
  
    return nil;
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if ([_type isEqualToString:@"comment"]) {
        
        return self.tableArray.count;
        
    } else if ([_type isEqualToString:@"information"]) {
    
        return 1;
    
    }
    return 0;
 
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([_type isEqualToString:@"comment"]) {
        
        if (_tableArray.count != 0) {
            
            
            NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
            CGFloat height = [self labelHeight: [dic objectForKey:@"content"] width:[[UIScreen mainScreen]bounds].size.width - 10];
            return height + 60;
            
        } else {
            
            return 100;
            
        }

        
    } else if ([_type isEqualToString:@"information"]) {
    
        CGFloat height = [self getContentHeight:[_dic objectForKey:@"NewsDetail"]];
        return 260 + height;
    
    }
    
    
    return 0;
}

- (CGFloat)getContentHeight:(NSString *)text
{
    
    CGSize size = CGSizeMake([[UIScreen mainScreen] bounds].size.width - 10, 10000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil] context:nil];
    return rect.size.height;
    
}



- (CGFloat)labelHeight:(NSString *)text width:(CGFloat)width
{
    
    CGSize size = CGSizeMake(width, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

@end
